package com.wanxi.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author Li
 * @Date 2023/5/4 16:17
 */
public class Tool {
    private Tool() {

    }

    public static Integer strToInt(String value) {
        try {
            return Integer.valueOf(value);

        } catch (Exception e) {
//            e.printStackTrace();
        }
        return -1;
    }

    //异常默认为第一页
    public static Integer nullToPage(String value) {
        try {
            return Integer.valueOf(value);

        } catch (Exception e) {
//        e.printStackTrace();
        }
        return 1;
    }

    public static Integer nullToLimit(String value) {
        try {
            return Integer.valueOf(value);

        } catch (Exception e) {
//            e.printStackTrace();
        }
        return 10;
    }

    //    为空转换为整型
    public static Integer nullToInt(String value) {
        try {
            return Integer.parseInt(value);
        } catch (Exception e) {
            return -1;
        }

    }

    //将null转换成空字符
    public static String nullToStr(String value) {

        return value == null ? "" : value;
    }


//日期转换

    public static String nullToDate(String value) {
        return value == null || "".equals(value) ? "1111-01-01" : value;
    }

    public static String strToDate(String value) {
        if (value == null) {
            return "1111-01-01";
        }
        String reg = "yyyy-MM-dd";
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(reg);
        try {
            Date date = simpleDateFormat.parse(value);
            return simpleDateFormat.format(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        return "1111-01-01";
    }
}
